var path = require("path");
var fs = require("fs");
// console.warn(process.argv)

var filePath = process.argv[2];
var lookingForString = process.argv[3];
recursiveReadFile(filePath);

function recursiveReadFile(fileName) {
  if (!fs.existsSync(fileName)) return;
  if (isFile(fileName)) {
    check(fileName);
  }
  if (isDirectory(fileName)) {
    var files = fs.readdirSync(fileName);
    files.forEach(function (val, key) {
      var temp = path.join(fileName, val);
      if (isDirectory(temp)) recursiveReadFile(temp);
      if (isFile(temp)) check(temp);
    })
  }
}
function check(fileName) {
  var data = readFile(fileName);
// console.warn(data)

  var exc = new RegExp(lookingForString);
  if (exc.test(data))
    console.log(fileName);

}
function isDirectory(fileName) {
  if (fs.existsSync(fileName)) return fs.statSync(fileName).isDirectory();
}
function isFile(fileName) {
  if (fs.existsSync(fileName)) return fs.statSync(fileName).isFile();
}
function readFile(fileName) {
  // console.log(fileName);
  
  if (fs.existsSync(fileName)) return fs.readFileSync(fileName, "utf-8");
}
// 使用例子记得特殊符号转义
// node find /Users/roft/Desktop/workspace/linyi-cli/libs "var exc = new RegExp\(lookingForString\)"
// cli find /Users/roft/Desktop/workspace/linyi-cli/libs "'var exc = new RegExp\(lookingForString\)'"

// 其他实践
// cli find /Users/roft/Desktop/workspace "'const { SIGN_OUT } = userCenter'"